{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## datetime \n",
    "### 操作日期和时间的package"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1、 主要介绍datetime和timedelta类型\n",
    "\n",
    "datetime是python中用来管理和表示时间的模块，可用的类型有：\n",
    "\n",
    "class datetime.date: 表示日期, 属性有: year, month 和 day。\n",
    "\n",
    "class datetime.time: 表示一天内的时间, 属性有: hour, minute, second, microsecond 和 tzinfo。\n",
    "\n",
    "class datetime.datetime: date和time合并, 属性有: year, month, day, hour, minute, second, microsecond 和 tzinfo。\n",
    "\n",
    "class datetime.timedelta:(date, time, datetime)一段时间\n",
    "\n",
    "class datetime.tzinfo: 用于表示时区的抽象类"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2、 datetime()参数\n",
    "\n",
    "class datetime.datetime(year, month, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**需要输入的参数有year, month和day, 其他为可选项。除tzinfo以外其他类型均为int或long**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**打印特定时间**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2017-01-02 03:04:05.000006\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "print(datetime(2017, 1, 2, 3, 4, 5, 6))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**返回当前时间**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2018-09-12 17:38:41.024984\n"
     ]
    }
   ],
   "source": [
    "print(datetime.now())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**返回当前UTC时间**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2018-09-12 09:38:41.031977\n"
     ]
    }
   ],
   "source": [
    "print(datetime.utcnow())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**从1970年1月1日0时开始所经历的秒数的当地时间**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2001-09-09 09:46:40\n"
     ]
    }
   ],
   "source": [
    "print(datetime.fromtimestamp(1000000000))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3、 格式转换 format"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'datetime.datetime'>\n"
     ]
    }
   ],
   "source": [
    "# 通过给定的format将date_string转换成datetime\n",
    "print(type(datetime.strptime(\"2017-01-01 01:02:03.000007\", \"%Y-%m-%d %H:%M:%S.%f\")))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20180912 17:38:41.054953\n"
     ]
    }
   ],
   "source": [
    "# 通过给定的format将datetime类型转换成string\n",
    "print(datetime.now().strftime(format=\"%Y%m%d %H:%M:%S.%f\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "format的格式详见： \n",
    "https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4、 迭代转换时间格式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from datetime import timedelta\n",
    "\n",
    "stock = pd.read_excel('sz50.xlsx',sheetname='600036.XSHG', index_col='datetime')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "stock.index = list(map(lambda x: x-timedelta(hours=15), stock.index))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             close    high     low    open    volume\n",
      "2017-11-14  111.81  112.81  110.57  110.93  42886800\n",
      "2017-11-15  111.25  112.73  110.21  111.65  34028800\n",
      "2017-11-16  112.13  112.13  109.73  110.77  33138100\n",
      "2017-11-17  117.24  117.67  112.93  112.93  74014200\n",
      "2017-11-20  121.82  122.50  116.20  116.92  64388900\n"
     ]
    }
   ],
   "source": [
    "print(stock.tail())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "datetime官方文档：https://docs.python.org/2/library/datetime.html"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5、 timedelta()\n",
    "\n",
    "class datetime.timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]])\n",
    "\n",
    "所有参数都不是必须的, 类型为int, long或float, 正负都可以。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "42 68157 999997\n"
     ]
    }
   ],
   "source": [
    "delta=timedelta(days=1, seconds=-2,microseconds=-3, minutes=-4, hours=-5, weeks=6)\n",
    "print(delta.days, delta.seconds, delta.microseconds)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6、 timedelta与datetime做简单的四则运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2018-09-12 17:39:11.506369\n",
      "1 day, 0:00:00\n",
      "2018-09-14 17:39:11.506369\n"
     ]
    }
   ],
   "source": [
    "now = datetime.now()\n",
    "td = timedelta(1)\n",
    "print (now)\n",
    "print (td)\n",
    "print (now + td * 2)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
